app.controller("purchaseAdd",function($scope,$http, $stateParams,get,post,$rootScope,Md5,$state){
	$scope.sexList=[{id:-1,name:"请选择"},{id:0,name:"是"},{id:1,name:'否'}]
	$scope.id=$stateParams.id
	//取消
	$scope.rement=function(){
		$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
	}
	$scope.selectss=function(){
		//项目名称
		get.list("dictionary/project").then(function(e){
				$scope.project = e
				$scope.project.unshift({
						diName:"请选择",
						diId:-1
					})
			})
		
		$scope.projectId={
						value:{
							diName:"请选择",
							diId:null
						}
					}
	}
	$scope.selectss();
	$scope.isshows=false
	$scope.add={};
	setTimeout(function (){
	if($stateParams.id!=''){
		get.list('purchase/findOne',{cId:$stateParams.id}).then(function(e){
							$scope.pickertime=e.cSignTime
							$scope.pickertimes=e.cMoney
							$scope.add.paymentTime=e.cMoney
							$scope.add.cId=e.cId
							$scope.add.projectId=e.projectId
							$scope.add.cSerialNum=e.cSerialNum
							$scope.add.cName=e.cName
							$scope.add.cPecific=e.cPecific
							$scope.add.cSupplier=e.cSupplier
							$scope.add.cAmount=e.cAmount||''
							$scope.add.cForecost=e.cForecost||''
							$scope.add.cCycle=e.cCycle||''
							$scope.add.cKind=e.cKind||''
							$scope.add.cPaymentPlan=e.cPaymentPlan||''
							$scope.add.cIfCompletePlan=e.cIfCompletePlan||''
							$scope.add.dateTime=e.SignTime
							$scope.add.cIfPayInvoice=e.cIfPayInvoice
							$scope.add.cRemark=e.cRemark||''
							$scope.add.payAcount=e.payAcount||''
							$scope.add.receivables=e.receivables||''
							$scope.add.companyPecificId=e.companyPecificId
							$scope.add.cCaigou=e.cCaigou||''
					//项目名称
					angular.forEach($scope.project,function(o){
						if(e.projectId==o.diId){
							$scope.projectId={
								value:o
							}
						}
					})
					
					
		})
	}else{
		//项目名称
			//项目类型
			$scope.companyPecificId={
						value:{
							diName:"请选择",
							diId:null
						}
					}
		/*$scope.add.cSerialNum="WXKJXM"+new Date().format("yyyyMMdd")*/
			$scope.add.pStatus=-1
			$scope.add.pBreakUp=-1
			$scope.add.cIfPayInvoice=-1
		
	}
	},200)
	$scope.$watch('add.projectId', function (value) {
		if(null!=value&&null!=value.diId){
			console.log(value.dgDecription)
			value.dgDecription=value.dgDecription.replace("XM",'')
			get.list("purchase/findlike",{cSerialNum:value.dgDecription}).then(function(e){
				/*if(e>0){
					$scope.add.cSerialNum=value.dgDecription+"-0"+e	
				}else{
					$scope.add.cSerialNum=value.dgDecription
				}*/
				$scope.add.cName=value.diName
			})
		
		}
	})
	$scope.sub=function(val){
		$scope.add.cType=27
		if($scope.add.projectId!=null){
			$scope.add.projectId=$scope.add.projectId.diId||$scope.add.projectId
		}
		$scope.add.dateTime=$scope.pickertime||$scope.add.dateTime
		$scope.add.paymentTime=$scope.add.working||$scope.add.paymentTime
		$scope.add.companyPecificId=$scope.add.companyPecificId||''
		$scope.add.cSupplier=$scope.add.cSupplier||''
			if($stateParams.id!=''){
				$scope.add.pId=$stateParams.id
				post.list("purchase/update",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
			}else{
				
				post.list("purchase/add",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
				
			}
			
			
		
		
	}
})
